<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/dute_favicon_32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/dute_favicon_16x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <link rel="manifest" href="/images/manifest.json">
  <meta name="msapplication-config" content="/images/browserconfig.xml">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="google-site-verification" content="mpI5dkydstZXl6UcDCppqktXK0bbvqdZ6LkZ3KNk4Iw">
  <meta name="baidu-site-verification" content="code-a1LksZX2Ds">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"whitestore.top","root":"/","scheme":"Gemini","version":"7.8.0","exturl":true,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="go语言学习">
<meta property="og:type" content="article">
<meta property="og:title" content="Go语言学习笔记（一）">
<meta property="og:url" content="https://whitestore.top/2020/07/26/gostudy1/index.html">
<meta property="og:site_name" content="爱看书的阿东">
<meta property="og:description" content="go语言学习">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://raw.githubusercontent.com/lazyTimes/imageRepository/master/img/20200216135342.png?ynotemdtimestamp=1595729096959">
<meta property="og:image" content="https://raw.githubusercontent.com/lazyTimes/imageRepository/master/img/20200216145822.png?ynotemdtimestamp=1595729096959">
<meta property="og:image" content="https://raw.githubusercontent.com/lazyTimes/imageRepository/master/img/20200216145942.png?ynotemdtimestamp=1595729096959">
<meta property="og:image" content="https://raw.githubusercontent.com/lazyTimes/imageRepository/master/img/20200216193505.png?ynotemdtimestamp=1595729096959">
<meta property="og:image" content="https://raw.githubusercontent.com/lazyTimes/imageRepository/master/img/20200216193610.png?ynotemdtimestamp=1595729096959">
<meta property="og:image" content="https://raw.githubusercontent.com/lazyTimes/imageRepository/master/img/20200216204755.png?ynotemdtimestamp=1595729096959">
<meta property="og:image" content="https://raw.githubusercontent.com/lazyTimes/imageRepository/master/img/20200217221715.png?ynotemdtimestamp=1595729096959">
<meta property="article:published_time" content="2020-07-26T03:33:37.000Z">
<meta property="article:modified_time" content="2023-07-16T06:28:09.222Z">
<meta property="article:author" content="阿东">
<meta property="article:tag" content="golang">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://raw.githubusercontent.com/lazyTimes/imageRepository/master/img/20200216135342.png?ynotemdtimestamp=1595729096959">

<link rel="canonical" href="https://whitestore.top/2020/07/26/gostudy1/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Go语言学习笔记（一） | 爱看书的阿东</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="爱看书的阿东" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">爱看书的阿东</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">赐他一块白色石头，石头上写着新名</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-fw fa-sitemap"></i>站点地图</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL2xhenlUaW1lcw==" title="Follow me on GitHub" aria-label="Follow me on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://whitestore.top/2020/07/26/gostudy1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="阿东">
      <meta itemprop="description" content="随遇而安">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="爱看书的阿东">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Go语言学习笔记（一）
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-07-26 11:33:37" itemprop="dateCreated datePublished" datetime="2020-07-26T11:33:37+08:00">2020-07-26</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2023-07-16 14:28:09" itemprop="dateModified" datetime="2023-07-16T14:28:09+08:00">2023-07-16</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Golang/" itemprop="url" rel="index"><span itemprop="name">Golang</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2020/07/26/gostudy1/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2020/07/26/gostudy1/" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>7.1k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>6 分钟</span>
            </span>
            <div class="post-description">go语言学习</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="Go语言学习笔记（一）"><a href="#Go语言学习笔记（一）" class="headerlink" title="Go语言学习笔记（一）"></a>Go语言学习笔记（一）</h1><h2 id="Go语言是什么？"><a href="#Go语言是什么？" class="headerlink" title="Go语言是什么？"></a>Go语言是什么？</h2><p><em>Go 编程语言是一个使得程序员更加有效率的开源项目。Go 是有表达力、简洁、清晰和有效率的。它的并行机制使其很容易编写多核和网络应用,而新奇的类型系统允许构建有弹性的模块化程序。Go 编译到机器码非常快速,同时具有便利的垃圾回收和强大的运行时反射。它是快速的、静态类型编译语言,但是感觉上是动态类型的,解释型语言。</em></p>
<a id="more"></a>

<h2 id="GO能做什么？"><a href="#GO能做什么？" class="headerlink" title="GO能做什么？"></a>GO能做什么？</h2><p><span class="exturl" data-url="aHR0cHM6Ly93d3cuemhpaHUuY29tL3F1ZXN0aW9uLzU3NDA0NTEy" title="https://www.zhihu.com/question/57404512">https://www.zhihu.com/question/57404512<i class="fa fa-external-link"></i></span> 知乎回答</p>
<h2 id="为什么要学习Go"><a href="#为什么要学习Go" class="headerlink" title="为什么要学习Go?"></a>为什么要学习Go?</h2><p>客观原因：</p>
<ol>
<li>GO 可以非常高效的完成一些JAVA需要很多功夫完成的操作</li>
</ol>
<p>例如：</p>
<ul>
<li>管理日志</li>
<li>IO监控</li>
<li>生成模板代码</li>
</ul>
<ol>
<li>近两年GO诞生的框架越来越多，他的便捷性得到认可</li>
<li>技多不压身</li>
</ol>
<p>主管原因：</p>
<ol>
<li>想用GO完成一些工作上重复干的活儿</li>
<li>编写一些工具帮助日常开发</li>
</ol>
<h2 id="如何学习？"><a href="#如何学习？" class="headerlink" title="如何学习？"></a>如何学习？</h2><p><span class="exturl" data-url="aHR0cHM6Ly93d3cuemhpaHUuY29tL3F1ZXN0aW9uLzIzNDg2MzQ0" title="https://www.zhihu.com/question/23486344">https://www.zhihu.com/question/23486344<i class="fa fa-external-link"></i></span> 知乎第二个</p>
<p><span class="exturl" data-url="aHR0cHM6Ly90b3VyLmdvLXpoLm9yZy9saXN0" title="https://tour.go-zh.org/list">https://tour.go-zh.org/list<i class="fa fa-external-link"></i></span> 快速入门（需要翻墙）</p>
<p><span class="exturl" data-url="aHR0cHM6Ly9taWtlc3Bvb2suY29tL2xlYXJuaW5nLWdvLw==" title="https://mikespook.com/learning-go/">https://mikespook.com/learning-go/<i class="fa fa-external-link"></i></span> 《学习GO语言》中文版，不过出版比较早，最好自我实践</p>
<p><span class="exturl" data-url="aHR0cHM6Ly9nb2J5ZXhhbXBsZS5jb20v" title="https://gobyexample.com/">https://gobyexample.com/<i class="fa fa-external-link"></i></span> 一些基础的语代码</p>
<h1 id="快速入门"><a href="#快速入门" class="headerlink" title="快速入门"></a>快速入门</h1><h2 id="安装GO"><a href="#安装GO" class="headerlink" title="安装GO"></a>安装GO</h2><p>进入官网：<span class="exturl" data-url="aHR0cHM6Ly9nb2xhbmcub3JnLw==" title="https://golang.org/">https://golang.org/<i class="fa fa-external-link"></i></span></p>
<p>（因为不可抗力的原因，这个房子国内没法访问。。。也可能是我联通网的问题）</p>
<p>window下载地址（注意32位和64位的区别）：</p>
<p><span class="exturl" data-url="aHR0cHM6Ly9kbC5nb29nbGUuY29tL2dvL2dvMS4xMy44LndpbmRvd3MtYW1kNjQubXNp" title="https://dl.google.com/go/go1.13.8.windows-amd64.msi">https://dl.google.com/go/go1.13.8.windows-amd64.msi<i class="fa fa-external-link"></i></span></p>
<p><span class="exturl" data-url="aHR0cHM6Ly9kbC5nb29nbGUuY29tL2dvL2dvMS4xMy44LndpbmRvd3MtMzg2Lm1zaQ==" title="https://dl.google.com/go/go1.13.8.windows-386.msi">https://dl.google.com/go/go1.13.8.windows-386.msi<i class="fa fa-external-link"></i></span></p>
<p>Linux下载地址（注意32位和64位的区别）：</p>
<p><span class="exturl" data-url="aHR0cHM6Ly9kbC5nb29nbGUuY29tL2dvL2dvMS4xMy44LnNyYy50YXIuZ3o=" title="https://dl.google.com/go/go1.13.8.src.tar.gz">https://dl.google.com/go/go1.13.8.src.tar.gz<i class="fa fa-external-link"></i></span></p>
<p><span class="exturl" data-url="aHR0cHM6Ly9kbC5nb29nbGUuY29tL2dvL2dvMS4xMy44LmRhcndpbi1hbWQ2NC50YXIuZ3o=" title="https://dl.google.com/go/go1.13.8.darwin-amd64.tar.gz">https://dl.google.com/go/go1.13.8.darwin-amd64.tar.gz<i class="fa fa-external-link"></i></span></p>
<h2 id="Go语言的特点"><a href="#Go语言的特点" class="headerlink" title="Go语言的特点"></a>Go语言的特点</h2><blockquote>
<p>并行 Go 让函数很容易成为 非常 轻量的线程。这些线程在 Go 中被叫做 goroutines</p>
</blockquote>
<blockquote>
<p>Channel 这些 goroutines 之间的通讯由 channel[18, 25] 完成；</p>
</blockquote>
<blockquote>
<p>快速 编译很快，执行也很快。目标是跟 C 一样快。编译时间用秒计算；</p>
</blockquote>
<blockquote>
<p>安全 当转换一个类型到另一个类型的时候需要显式的转换并遵循严格的规则。Go 有垃圾收集，在 Go 中无须 free() ，语言会处理这一切；</p>
</blockquote>
<blockquote>
<p>标准格式化 Go 程序可以被格式化为程序员希望的（几乎）任何形式，但是官方格式是存在的。标准也非常简单： gofmt 的输出就是 官方认可的格式 ；</p>
</blockquote>
<blockquote>
<p>类型后置 类型在变量名的 后面 ，像这样 var a int ，来代替 C 中的 int a ；UTF-8 任何地方都是 UTF-8 的，包括字符串 以及 程序代码。你可以在代码中使用 Φ = Φ + 1 ；</p>
</blockquote>
<blockquote>
<p>开源 Go 的许可证是完全开源的，参阅 Go 发布的源码中的 LICENSE 文件；</p>
</blockquote>
<blockquote>
<p>开心 用 Go 写程序会非常开心！</p>
</blockquote>
<h2 id="IDEA-配置GO语言支持"><a href="#IDEA-配置GO语言支持" class="headerlink" title="IDEA 配置GO语言支持"></a>IDEA 配置GO语言支持</h2><ol>
<li>打开<code>Setting</code></li>
<li>选择<code>Setting</code>，输入<code>GO</code></li>
<li>安装<code>GO</code>语言支持</li>
</ol>
<p><img src="https://raw.githubusercontent.com/lazyTimes/imageRepository/master/img/20200216135342.png?ynotemdtimestamp=1595729096959" alt="img"></p>
<ol>
<li>同时安装GO的Template的插件</li>
<li>重启IDEA</li>
</ol>
<h2 id="IDEA-创建GO项目"><a href="#IDEA-创建GO项目" class="headerlink" title="IDEA 创建GO项目"></a>IDEA 创建GO项目</h2><p>完成上一步的操作之后，我们可以实现IDEA创建GO项目</p>
<ol>
<li>打开IDEA，左上角<code>File</code>，New-&gt;选择GO工程</li>
</ol>
<p><img src="https://raw.githubusercontent.com/lazyTimes/imageRepository/master/img/20200216145822.png?ynotemdtimestamp=1595729096959" alt="img"></p>
<ol>
<li>自定义一个项目名称</li>
</ol>
<p><img src="https://raw.githubusercontent.com/lazyTimes/imageRepository/master/img/20200216145942.png?ynotemdtimestamp=1595729096959" alt="img"></p>
<ol>
<li>创建一个HelloWorld包</li>
<li>写入如下代码</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">package main</span><br><span class="line"></span><br><span class="line">import &quot;fmt&quot;</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">	fmt.Print(&quot;hello&quot;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol>
<li>写好之后，便可以直接右上角运行</li>
</ol>
<p>以上便是一个最简单的GO语言Helloworld代码，感觉比较懵逼，工具做了哪些事情？</p>
<h3 id="使用原始的方式构建一个helloword"><a href="#使用原始的方式构建一个helloword" class="headerlink" title="使用原始的方式构建一个helloword"></a>使用原始的方式构建一个helloword</h3><p>使用原始的GO语言方式需要满足前提条件：<strong>环境变量是否设置</strong></p>
<ol>
<li>在指定的目录比如<code>D:\java\GO\go-study\helloword</code>下面创建一个<code>hello.go</code>的文件</li>
<li>使用<code>NotePad++</code>编写脚本</li>
<li>和上面的代码一样，照抄或者复制粘贴，这里直接复制了上面的代码</li>
<li>打开<code>cmd</code>，进入对应的目录<code>cd D:\java\GO\go-study\helloword</code>，<strong>注意切换一下盘符</strong></li>
<li>执行命令<code>go run hello.go</code>，即可看到结果</li>
</ol>
<blockquote>
<p>早期的GO语言需要类似<code>javac</code>的编译动作，但是现在没有这个必要了</p>
<p>其他情况：</p>
<ul>
<li>Go 被安装在 ˜/go ，而 $GOROOT 被设置为 GOROOT=˜/go ；</li>
<li>希望编译的 Go 代码放在 ˜<strong>/g/src</strong> 而 $GOPATH 设置为 <strong>GOPATH=˜/g</strong> 。在使用包的 时候需要用到这个变量（参阅第 3 章）。</li>
</ul>
</blockquote>
<h2 id="注意事项"><a href="#注意事项" class="headerlink" title="注意事项:"></a>注意事项:</h2><ol>
<li><code>package main</code>必须首先出现，紧跟着是<code>import</code></li>
<li>首行这个是必须的。所有的 Go 文件以 <code>package &lt;something&gt;</code>开头，对于独立运行的执行文件必须是 <code>package main</code>；</li>
</ol>
<h1 id="了解GO语言"><a href="#了解GO语言" class="headerlink" title="了解GO语言"></a>了解GO语言</h1><h2 id="变量定义"><a href="#变量定义" class="headerlink" title="变量定义"></a>变量定义</h2><ul>
<li>需要先声明类型，注意和JAVA完全不一样：例如<code>var a int</code></li>
<li>紧接着<code>:=</code>方式设置变量，l例如：<code>a := 15</code></li>
</ul>
<blockquote>
<p>Golang 在变量定义的时候设置了默认值，和JAVA的区别在于使用默认赋值</p>
<p>同时要注意一下：如果在函数体内部定义变量不使用GO会产生一个报错</p>
</blockquote>
<p>注意：</p>
<ul>
<li><code>dd:=1</code>这种形式只能在函数体内部</li>
<li><code>ar ss = &quot;sssdd&quot;</code> 会根据赋值猜测变量类型</li>
</ul>
<h3 id="定义多个变量"><a href="#定义多个变量" class="headerlink" title="定义多个变量"></a>定义多个变量</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">var (</span><br><span class="line">	x int</span><br><span class="line">	b bool</span><br><span class="line">)</span><br></pre></td></tr></table></figure>

<h3 id="平行声明和赋值"><a href="#平行声明和赋值" class="headerlink" title="平行声明和赋值"></a>平行声明和赋值</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 平行声明</span><br><span class="line">var a,b int </span><br><span class="line">&#x2F;&#x2F; 平行赋值</span><br><span class="line">a,b :&#x3D; 34, 35</span><br></pre></td></tr></table></figure>

<h3 id="明确变量长度"><a href="#明确变量长度" class="headerlink" title="明确变量长度"></a>明确变量长度</h3><p>如果你希望明确其长度，可以使用如下的定义</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">var df int32 &#x3D; 55;</span><br></pre></td></tr></table></figure>

<p>注意：默认的通用int会根据硬件去判断32位还是64位</p>
<p>其他定义举例：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int8 ， int16 ， int32 ， int64 和 byte ， uint8 ， uint16 ， uint32 ，uint64</span><br></pre></td></tr></table></figure>

<p><strong>其中byte 是 uint8 的别名</strong></p>
<h3 id="可以混合使用类型吗？"><a href="#可以混合使用类型吗？" class="headerlink" title="可以混合使用类型吗？"></a>可以混合使用类型吗？</h3><p>来看下面的定义</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">1 package main</span><br><span class="line">2</span><br><span class="line">3 func main() &#123;</span><br><span class="line">4 	var a int ← 通用整数类型</span><br><span class="line">5 	var b int32 ← 32 位整数类型</span><br><span class="line">6 	a &#x3D; 15</span><br><span class="line">7 	b &#x3D; a + a ← 混合这些类型是非法的</span><br><span class="line">8 	b &#x3D; b + 5 ← 5 是一个（未定义类型的）常量，所以这没?问题</span><br><span class="line">9 &#125;</span><br></pre></td></tr></table></figure>

<p><code>types.go:7: cannot use a + a (type int) as type int32 in assignment</code> 赋值非法的时候运行会报错，编译器会提示语法错误</p>
<h2 id="常量定义"><a href="#常量定义" class="headerlink" title="常量定义"></a>常量定义</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">const (</span><br><span class="line">    a &#x3D; iota</span><br><span class="line">    b &#x3D; iota</span><br><span class="line">)</span><br></pre></td></tr></table></figure>

<p>第一个 iota 表示为 0，因此 a 等于 0，当 iota 再次在新的一行使用时，它的值增加了 1，因此 b 的值是 1</p>
<p>省略重复的<code>iota</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">const (</span><br><span class="line">    a &#x3D; iota</span><br><span class="line">    b</span><br><span class="line">)</span><br></pre></td></tr></table></figure>

<p>指定确切的类型</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">const (</span><br><span class="line">    a &#x3D; 0 ← Is an int now</span><br><span class="line">    b string &#x3D; &quot;0&quot;</span><br><span class="line">)</span><br></pre></td></tr></table></figure>

<h3 id="字符串"><a href="#字符串" class="headerlink" title="字符串"></a>字符串</h3><p><code>：</code>在 Go 中字符串是不可变的，字符串的修改需要如下方法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">s :&#x3D; &quot;hello&quot; </span><br><span class="line">c :&#x3D; [] rune (s) &#x2F;&#x2F; 转换 s 为 rune 数组</span><br><span class="line">c[0] &#x3D; &#39;c&#39;&#x2F;&#x2F; 修改数组的第一个元素</span><br><span class="line">s2 :&#x3D; string (c) &#x2F;&#x2F;创建 新的 字符串 s2 保存修改</span><br><span class="line">fmt.Printf(&quot;%s\n&quot;, s2) &#x2F;&#x2F;用 fmt.Printf 函数输出字符串</span><br></pre></td></tr></table></figure>

<p>多行文本的写法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">s:&#x3D; &quot;sdsdadsa &quot;+ </span><br><span class="line">&quot;asdsad&quot; + </span><br><span class="line">&quot;sddsd&quot;</span><br></pre></td></tr></table></figure>

<p>注意<code>&quot;+&quot;</code>号必须在末尾</p>
<h3 id="复数"><a href="#复数" class="headerlink" title="复数"></a>复数</h3><p>Go 原生支持复数，写法如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var c complex64 &#x3D; 5+5i ; </span><br><span class="line">fmt.Printf(&quot;Value is: %v&quot;, c)</span><br></pre></td></tr></table></figure>

<h3 id="错误"><a href="#错误" class="headerlink" title="错误"></a>错误</h3><p>在GO里面，错误也被当做类型处理，声明的方式如下:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">var e error</span><br></pre></td></tr></table></figure>

<p>默认为<code>null</code>， 本质上是一个接口</p>
<h2 id="运算符和内建函数"><a href="#运算符和内建函数" class="headerlink" title="运算符和内建函数"></a>运算符和内建函数</h2><p><img src="https://raw.githubusercontent.com/lazyTimes/imageRepository/master/img/20200216193505.png?ynotemdtimestamp=1595729096959" alt="img"></p>
<blockquote>
<p>Go <strong>不支持</strong>运算符重载</p>
</blockquote>
<h2 id="关键字"><a href="#关键字" class="headerlink" title="关键字"></a>关键字</h2><p><img src="https://raw.githubusercontent.com/lazyTimes/imageRepository/master/img/20200216193610.png?ynotemdtimestamp=1595729096959" alt="img"></p>
<p>先做个大概了解，详细使用的时候在记录笔记</p>
<h2 id="控制语句"><a href="#控制语句" class="headerlink" title="控制语句"></a>控制语句</h2><h3 id="if-else"><a href="#if-else" class="headerlink" title="if-else"></a>if-else</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">if (true &amp;&amp; true) </span><br><span class="line">&#123;</span><br><span class="line">    fmt.Print(&quot;hello&quot;)</span><br><span class="line">    return</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注意格式，这种写法是不被允许的，需要将第一个左括号放到<code>if</code>的同一行</p>
<h3 id="goto"><a href="#goto" class="headerlink" title="goto"></a>goto</h3><p>写法如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">func myfunc() &#123;</span><br><span class="line">	i :&#x3D; 0</span><br><span class="line">Here: ← 这行的第一个词，以分号结束作为标签</span><br><span class="line">    println(i)</span><br><span class="line">    i++</span><br><span class="line">    goto Here ← 跳转</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>标签<strong>大小写敏感</strong></p>
<h3 id="for"><a href="#for" class="headerlink" title="for"></a>for</h3><p>写法分成三种</p>
<p>第一种：一般的for循环</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">var i &#x3D; 5</span><br><span class="line">Tag:</span><br><span class="line">    if i &#x3D;&#x3D; 0 &#123;</span><br><span class="line">        return</span><br><span class="line">    &#125;</span><br><span class="line">for i :&#x3D; 0; i&lt;10 ; i++ &#123;</span><br><span class="line">    fmt.Println(i);</span><br><span class="line">&#125;</span><br><span class="line">i--</span><br><span class="line">goto Tag</span><br></pre></td></tr></table></figure>

<p>第二种：相当于while</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">var z &#x3D; 6</span><br><span class="line">for z &gt; 5 &#123;</span><br><span class="line">    z++</span><br><span class="line">    if z &gt; 7 &#123;</span><br><span class="line">        break</span><br><span class="line">    &#125;</span><br><span class="line">    print(&quot;hello wolrd&quot;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>第三种：表示死循环</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">for &#123;</span><br><span class="line">    .....</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注意下面的写法是固定的，不能使用逗号表达式，只能使用平行赋值的方式</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">for i, j :&#x3D; 0, len (a)-1 ; i &lt; j ; i, j &#x3D; i+1, j-1 &#123;</span><br><span class="line">	a[i], a[j] &#x3D; a[j], a[i] ← 平行赋值</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>注意GO 里面是没有while语句的</p>
<p>只能使用for进行书写while的循环</p>
</blockquote>
<h3 id="range"><a href="#range" class="headerlink" title="range"></a>range</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">list :&#x3D; []string&#123;&quot;aaa&quot;, &quot;vvv&quot;, &quot;ccc&quot;, &quot;ddd&quot;, &quot;eeee&quot;&#125;</span><br><span class="line">	for k,v :&#x3D; range list &#123;</span><br><span class="line">		print(k, &quot;-&quot;, v)</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>

<p>上面的例子是如下运转的：</p>
<ol>
<li>k 表示为 下标， v为值， range 代表了迭代器</li>
<li>迭代元素从 0 到 4 ，元素从 aaa…eeee 迭代</li>
</ol>
<p>也可以在字符串上直接使用 range ，看下面的案例</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="switch"><a href="#switch" class="headerlink" title="switch"></a>switch</h2><p>switch非常的灵活，可以在case里面写上表达式</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">func unhex(c byte ) byte &#123;</span><br><span class="line">    switch &#123;</span><br><span class="line">    case &#39;0&#39; &lt;&#x3D; c &amp;&amp; c &lt;&#x3D; &#39;9&#39;:</span><br><span class="line">    return c - &#39;0&#39;</span><br><span class="line">    case &#39;a&#39; &lt;&#x3D; c &amp;&amp; c &lt;&#x3D; &#39;f&#39;:</span><br><span class="line">    return c - &#39;a&#39; + 10</span><br><span class="line">    case &#39;A&#39; &lt;&#x3D; c &amp;&amp; c &lt;&#x3D; &#39;F&#39;:</span><br><span class="line">    return c - &#39;A&#39; + 10</span><br><span class="line">    &#125;</span><br><span class="line">    return 0</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>它不会匹配失败后自动向下尝试，但是可以使用 fallthrough 使其这样做</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">switch i &#123;</span><br><span class="line">case 0: fallthrough</span><br><span class="line">case 1:</span><br><span class="line">f() &#x2F;&#x2F; 当 i &#x3D;&#x3D; 0 时， f 会被调用！</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>还可以使用下面这张写法，逗号分隔多个条件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">case &#39; &#39;, &#39;?&#39;, &#39;&amp;&#39;, &#39;&#x3D;&#39;, &#39;#&#39;, &#39;+&#39;: ← , as ”or”</span><br><span class="line">return true</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="内建函数"><a href="#内建函数" class="headerlink" title="内建函数"></a>内建函数</h2><p><img src="https://raw.githubusercontent.com/lazyTimes/imageRepository/master/img/20200216204755.png?ynotemdtimestamp=1595729096959" alt="img"></p>
<h3 id="array、slices-和-map"><a href="#array、slices-和-map" class="headerlink" title="array、slices 和 map"></a>array、slices 和 map</h3><h4 id="array"><a href="#array" class="headerlink" title="array"></a>array</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">var arr &#x3D; [3]int&#123;&#125;</span><br><span class="line">for item :&#x3D; range arr&#123;</span><br><span class="line">    print(item);</span><br><span class="line">&#125;</span><br><span class="line">ss :&#x3D; [...] int&#123;1,2,2,3,4,4&#125;</span><br><span class="line">&#x2F;&#x2F; 初始化</span><br><span class="line">a :&#x3D; [3] int&#123; 1, 2, 3&#125;</span><br><span class="line">ff :&#x3D; [3][2] int &#123; &#123; 1,2 &#125; , &#123; 3,4 &#125; , &#123; 5,6 &#125; &#125;</span><br><span class="line">bb :&#x3D; [3][2] int &#123; [2] int &#123; 1,2 &#125; , [2] int &#123; 3,4 &#125; , [2] int &#123; 5,6 &#125; &#125;</span><br><span class="line">print(a, bb, ss);</span><br></pre></td></tr></table></figure>

<h4 id="slices"><a href="#slices" class="headerlink" title="slices"></a>slices</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">slice&#96;总是指向底层的一个&#96;array</span><br></pre></td></tr></table></figure>

<p>总体来说go的语法基本和java相符，但是部分内容需要小心对待，个人不是很爽go语言的部分奇葩语法</p>
<p>slice 总是与一个固定长度的 array 成对出现。其影响 slice 的容量和长度</p>
<p>slice： slice := array[0:n]</p>
<p>对比图；</p>
<p><img src="https://raw.githubusercontent.com/lazyTimes/imageRepository/master/img/20200217221715.png?ynotemdtimestamp=1595729096959" alt="img"></p>
<h5 id="扩展-slice"><a href="#扩展-slice" class="headerlink" title="扩展 slice"></a>扩展 slice</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">append&#96; 和 &#96;copy</span><br></pre></td></tr></table></figure>

<p>函数<code>append</code> 向<code>slice s</code>追加零值或其他 <code>x</code>值，并且返回追加后的新的、与 s 有相同类型的<code>slice</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">s0 :&#x3D; [] int &#123; 0, 0 &#125;  </span><br><span class="line">s1 :&#x3D; append (s0, 2) &#x2F;&#x2F;追加一个元素， s1 &#x3D;&#x3D; []int&#123;0, 0, 2&#125; ；</span><br><span class="line">s2 :&#x3D; append (s1, 3, 5, 7) &#x2F;&#x2F;追加多个元素， s2 &#x3D;&#x3D; []int&#123;0, 0, 2, 3, 5, 7&#125; ；</span><br><span class="line">s3 :&#x3D; append (s2, s0...) &#x2F;&#x2F; 追加一个 slice， s3 &#x3D;&#x3D; []int&#123;0, 0, 2, 3, 5, 7, 0, 0&#125; 。注意这三个点！</span><br></pre></td></tr></table></figure>

<p>函数 copy 从源 slice src 复制元素到目标 dst ，并且返回复制的元素的个数。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">var a &#x3D; [...] int &#123; 0, 1, 2, 3, 4, 5, 6, 7 &#125;</span><br><span class="line">var s &#x3D; make ([] int , 6)</span><br><span class="line">n1 :&#x3D; copy (s, a[0:]) ← n1 &#x3D;&#x3D; 6, s &#x3D;&#x3D; []int&#123;0, 1, 2, 3, 4, 5&#125;</span><br><span class="line">n2 :&#x3D; copy (s, s[2:]) ← n2 &#x3D;&#x3D; 4, s &#x3D;&#x3D; []int&#123;2, 3, 4, 5, 4, 5&#125;</span><br></pre></td></tr></table></figure>

<h4 id="map"><a href="#map" class="headerlink" title="map"></a>map</h4><p>案例：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">monthdays :&#x3D; map [ string ] int &#123;</span><br><span class="line">&quot;Jan&quot;: 31, &quot;Feb&quot;: 28, &quot;Mar&quot;: 31,</span><br><span class="line">&quot;Apr&quot;: 30, &quot;May&quot;: 31, &quot;Jun&quot;: 30,</span><br><span class="line">&quot;Jul&quot;: 31, &quot;Aug&quot;: 31, &quot;Sep&quot;: 30,</span><br><span class="line">&quot;Oct&quot;: 31, &quot;Nov&quot;: 30, &quot;Dec&quot;: 31, ← 逗号是必须的</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>只做声明的时候：<code>monthdays := make ( map[ string ] int )</code></p>
<p>如何使用：<code>monthdays[&quot;Dec&quot;]</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">for _, days :&#x3D; range monthdays &#123; ← 键没有使用，因此用 _, days</span><br><span class="line">year +&#x3D; days</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>检查元素是否存在</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">var value int</span><br><span class="line">var present bool</span><br><span class="line">value, present &#x3D; monthdays[&quot;Jan&quot;] ← 如果存在， present 则有值 true</span><br><span class="line">← 或者更接近 Go 的方式</span><br><span class="line">v, ok :&#x3D; monthdays[&quot;Jan&quot;] ← “逗号 ok ”形式</span><br></pre></td></tr></table></figure>

<h1 id="练习"><a href="#练习" class="headerlink" title="练习"></a>练习</h1><h2 id="Q1-0-For-loop"><a href="#Q1-0-For-loop" class="headerlink" title="Q1. (0) For-loop"></a>Q1. (0) For-loop</h2><ol>
<li>创建一个基于 for 的简单的循环。使其循环 10 次，并且使用 fmt 包打印出计数 器的值。</li>
<li>用 goto 改写 1 的循环。关键字 for 不可使用。</li>
<li>再次改写这个循环，使其遍历一个 array，并将这个 array 打印到屏幕上。</li>
</ol>
<h1 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">0-func 1-(p mytype) 2-funcname(3- q int ) (r,s int ) &#123; return 0,0 &#125;</span><br></pre></td></tr></table></figure>

<p>. . 0 关键字 func 用于定义一个函数； . . 1 函数可以绑定到特定的类型上。这叫做 接收者 。有接收者的函数被称作 method。 第 5 章将对其进行说明； . . 2 funcname 是你函数的名字； . . 3 int 类型的变量 q 作为输入参数。参数用 pass-by-value 方式传递，意味着它们会 被复制； . . 4 变量 r 和 s 是这个函数的 命名返回值。在 Go 的函数中可以返回多个值。参阅 第 28 页的 “多值返回”。如果不想对返回的参数命名，只需要提供类型： ( int , int ) 。 如果只有一个返回值，可以省略圆括号。如果函数是一个子过程，并且没有任何 返回值，也可以省略这些内容； . . 5 这是函数体。注意 return 是一个语句，所以包裹参数的括号是可选的。</p>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kity@2.0.4/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kityminder-core@1.4.50/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.css">
    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>lazytime
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://whitestore.top/2020/07/26/gostudy1/" title="Go语言学习笔记（一）">https://whitestore.top/2020/07/26/gostudy1/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLzQuMC96aC1DTg=="><i class="fa fa-fw fa-creative-commons"></i>BY-NC</span> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Golang/" rel="tag"># Golang</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/07/26/picgomayun/" rel="prev" title="PicGo和码云实现自建图床">
      <i class="fa fa-chevron-left"></i> PicGo和码云实现自建图床
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/07/26/jdkymjd/" rel="next" title="JDK源码解读">
      JDK源码解读 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Go语言学习笔记（一）"><span class="nav-number">1.</span> <span class="nav-text">Go语言学习笔记（一）</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Go语言是什么？"><span class="nav-number">1.1.</span> <span class="nav-text">Go语言是什么？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#GO能做什么？"><span class="nav-number">1.2.</span> <span class="nav-text">GO能做什么？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#为什么要学习Go"><span class="nav-number">1.3.</span> <span class="nav-text">为什么要学习Go?</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#如何学习？"><span class="nav-number">1.4.</span> <span class="nav-text">如何学习？</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#快速入门"><span class="nav-number">2.</span> <span class="nav-text">快速入门</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#安装GO"><span class="nav-number">2.1.</span> <span class="nav-text">安装GO</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Go语言的特点"><span class="nav-number">2.2.</span> <span class="nav-text">Go语言的特点</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#IDEA-配置GO语言支持"><span class="nav-number">2.3.</span> <span class="nav-text">IDEA 配置GO语言支持</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#IDEA-创建GO项目"><span class="nav-number">2.4.</span> <span class="nav-text">IDEA 创建GO项目</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#使用原始的方式构建一个helloword"><span class="nav-number">2.4.1.</span> <span class="nav-text">使用原始的方式构建一个helloword</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#注意事项"><span class="nav-number">2.5.</span> <span class="nav-text">注意事项:</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#了解GO语言"><span class="nav-number">3.</span> <span class="nav-text">了解GO语言</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#变量定义"><span class="nav-number">3.1.</span> <span class="nav-text">变量定义</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#定义多个变量"><span class="nav-number">3.1.1.</span> <span class="nav-text">定义多个变量</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#平行声明和赋值"><span class="nav-number">3.1.2.</span> <span class="nav-text">平行声明和赋值</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#明确变量长度"><span class="nav-number">3.1.3.</span> <span class="nav-text">明确变量长度</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#可以混合使用类型吗？"><span class="nav-number">3.1.4.</span> <span class="nav-text">可以混合使用类型吗？</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#常量定义"><span class="nav-number">3.2.</span> <span class="nav-text">常量定义</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#字符串"><span class="nav-number">3.2.1.</span> <span class="nav-text">字符串</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#复数"><span class="nav-number">3.2.2.</span> <span class="nav-text">复数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#错误"><span class="nav-number">3.2.3.</span> <span class="nav-text">错误</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#运算符和内建函数"><span class="nav-number">3.3.</span> <span class="nav-text">运算符和内建函数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#关键字"><span class="nav-number">3.4.</span> <span class="nav-text">关键字</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#控制语句"><span class="nav-number">3.5.</span> <span class="nav-text">控制语句</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#if-else"><span class="nav-number">3.5.1.</span> <span class="nav-text">if-else</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#goto"><span class="nav-number">3.5.2.</span> <span class="nav-text">goto</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#for"><span class="nav-number">3.5.3.</span> <span class="nav-text">for</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#range"><span class="nav-number">3.5.4.</span> <span class="nav-text">range</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#switch"><span class="nav-number">3.6.</span> <span class="nav-text">switch</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#内建函数"><span class="nav-number">3.7.</span> <span class="nav-text">内建函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#array、slices-和-map"><span class="nav-number">3.7.1.</span> <span class="nav-text">array、slices 和 map</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#array"><span class="nav-number">3.7.1.1.</span> <span class="nav-text">array</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#slices"><span class="nav-number">3.7.1.2.</span> <span class="nav-text">slices</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#扩展-slice"><span class="nav-number">3.7.1.2.1.</span> <span class="nav-text">扩展 slice</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#map"><span class="nav-number">3.7.1.3.</span> <span class="nav-text">map</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#练习"><span class="nav-number">4.</span> <span class="nav-text">练习</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Q1-0-For-loop"><span class="nav-number">4.1.</span> <span class="nav-text">Q1. (0) For-loop</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#函数"><span class="nav-number">5.</span> <span class="nav-text">函数</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">阿东</p>
  <div class="site-description" itemprop="description">随遇而安</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">239</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">36</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">37</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2xhenlUaW1lcw==" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;lazyTimes"><i class="fa fa-fw fa-github"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="bWFpbHRvOjEwOTc0ODM1MDhAcXEuY29t" title="E-Mail → mailto:1097483508@qq.com"><i class="fa fa-fw fa-envelope"></i>E-Mail</span>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title">
      <i class="fa fa-fw fa-link"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly93d3cuNTJwb2ppZS5jbi9ob21lLnBocD9tb2Q9c3BhY2UmdWlkPTE0OTc3MTgmZG89dGhyZWFkJnZpZXc9bWUmZnJvbT1zcGFjZQ==" title="https:&#x2F;&#x2F;www.52pojie.cn&#x2F;home.php?mod&#x3D;space&amp;uid&#x3D;1497718&amp;do&#x3D;thread&amp;view&#x3D;me&amp;from&#x3D;space">吾爱破解</span>
        </li>
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly9qdWVqaW4uaW0vdXNlci8yOTk5MTIzNDUyNjI2MzY2" title="https:&#x2F;&#x2F;juejin.im&#x2F;user&#x2F;2999123452626366">掘金</span>
        </li>
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly9zZWdtZW50ZmF1bHQuY29tL3UvbGF6eXRpbWVz" title="https:&#x2F;&#x2F;segmentfault.com&#x2F;u&#x2F;lazytimes">思否</span>
        </li>
    </ul>
  </div>

      </div>

      <div class="wechat_OA">
        <span>欢迎关注我的公众号</span>
        <br>
          <!-- 这里添加你的二维码图片 -->
        <img src ="https://adong-picture.oss-cn-shenzhen.aliyuncs.com/adong/wechat_channel.jpg">
      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">阿东</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">2m</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">29:50</span>
</div>
  <div class="powered-by">由 <span class="exturl theme-link" data-url="aHR0cHM6Ly9oZXhvLmlv">Hexo</span> & <span class="exturl theme-link" data-url="aHR0cHM6Ly90aGVtZS1uZXh0Lm9yZw==">NexT.Gemini</span> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : true,
      appId      : 'qMUpEEvBgXaMDD1b0ftgi9xr-gzGzoHsz',
      appKey     : 'UCdfT4Rfih6MO6y8DI4fstf6',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : 'zh-CN' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
